import java.util.Arrays;
import java.util.Scanner;
public class 仓库选址 {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int n= scan.nextInt();
        int []A=new int[n];
        for (int i = 0; i < n; i++) {
            A[i]=scan.nextInt();
        }
        //思路：把仓库放中间即可
        Arrays.sort(A);
        int res=0;
        int mid=A[n/2];
        for (int i = 0; i < n/2; i++) {
            res+=mid-A[i];
        }
        for (int i = n/2; i < n; i++) {
            res+=A[i]-mid;
        }
        System.out.println(res);
    }
}
